import unittest
from api.login import LoginApi
from api.employee import EmployeeApi


# 登录存储token
class TestLogin(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.login_api = LoginApi()

    def test01_login001(self):
        response = self.login_api.login({
            'mobile': '13800000002',
            'password': '123456'
        })
        json_data = response.json()
        self.assertEqual(200, response.status_code)
        self.assertEqual(True, json_data.get('success'))
        self.assertEqual(10000, json_data.get('code'))
        self.assertIn('操作成功', json_data.get('message'))
        if json_data.get('code') == 10000 and '操作成功' in json_data.get('message'):
            with open('./data/token.text', 'w', encoding='utf-8') as f:
                f.write(json_data.get('data'))


# 员工测试用例
class TestAddEmployee(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.employee_api = EmployeeApi()
        # cls.test_login = TestLogin()

    def test01_employee001(self):
        # 增加
        # response = self.employee_api.add_employee({
        #     'username': '刘镇赫1',
        #     'mobile': '18700011013',
        #     'workNumber': '100158'
        # })
        # json_data = response.json()
        # self.assertEqual(200, response.status_code)
        # self.assertEqual(True, json_data.get('success'))
        # self.assertEqual(10000, json_data.get('code'))
        # self.assertIn('操作成功', json_data.get('message'))
        # data = json_data.get('data')
        # emp_id = data.get('id')

        # 修改
        response2 = self.employee_api.update_employee(1063705989926227968, {
            'username': '阿打算打算复读'
        })
        json_data2 = response2.json()
        self.assertEqual(200, response2.status_code)
        self.assertEqual(True, json_data2.get('success'))
        self.assertEqual(10000, json_data2.get('code'))
        self.assertIn('操作成功', json_data2.get('message'))

        # # 查询
        # response1 = self.employee_api.get_employee(emp_id)
        # json_data1 = response1.json()
        # self.assertEqual(200, response1.status_code)
        # self.assertEqual(True, json_data1.get('success'))
        # self.assertEqual(10000, json_data1.get('code'))
        # self.assertIn('操作成功', json_data1.get('message'))
        #
        # # 删除
        # response3 = self.employee_api.delete_employee(emp_id)
        # json_data3 = response3.json()
        # self.assertEqual(200, response3.status_code)
        # self.assertEqual(True, json_data3.get('success'))
        # self.assertEqual(10000, json_data3.get('code'))
        # self.assertIn('操作成功', json_data3.get('message'))
